#include "SkillEarthquake.h"
#include "../Supporting/Enums/EnumSkillType.h"
#include "../Cats/CatsManager.h"
#include "../Cats/BaseCat.h"
#include "../Map/GameMap.h"
#include "../Supporting/CCShake.h"
#include "../Supporting/SoundLoader.h"
#include "../Supporting/Enums/EnumSoundType.h"

using namespace cocos2d;

//===== CONSTRUCTOR =====
SkillEarthquake::SkillEarthquake() : BaseSkill()
{
	_skillType = SKILL_EARTHQUAKE;
	_skillDamage = 4;
	_chargePointRequired = 8;
}
//===== DESTRUCTOR ======
SkillEarthquake::~SkillEarthquake()
{
}

/*
 * @function: loadSkill
 * @param: filename of the skill button image to load
 * @brief: Create and load the skill as a CCSprite
 * @result: All required data loaded into memory
 */
BaseSkill* SkillEarthquake::loadSkill(cocos2d::CCString filename)
{
	SkillEarthquake *skill = new SkillEarthquake();

	if( skill && skill->initWithFile(filename.getCString()) )
	{
		skill->setColor(ccc3(100,100,100));
		skill->autorelease();
		return skill;
	}

	CC_SAFE_DELETE(skill);
	return NULL;
}

/*
 * @function: didAccelerate
 * @brief: Cocos2d-x default library for accelerometer, used to control earthquake skill
 * 			If user shake the phone, the skill be activated and all cats are damaged
 * @result: All cats are damaged when skill is activated
 */
void SkillEarthquake::didAccelerate(cocos2d::CCAcceleration* pAccelerationVal, CatsManager* pCatsRef)
{
    float accX = pAccelerationVal->x;                //horizontal acceleration
    float accY = pAccelerationVal->y;                //vertical acceleration

    //Check if accelerometer has detected a shake motion pass a certain threshold
    if( _isSkillActivated && ( accX > accelerometerThreshold || accY > accelerometerThreshold ) )
    {
		SoundLoader::playSound(SKILL_EARTHQUAKE_SOUND);

		CCArray *_cats = pCatsRef->returnCats();
		for( int i = 0 ; i < _cats->count() ; i++ )
		{
			BaseCat *cat = (BaseCat*)_cats->objectAtIndex(i);

			if (cat == NULL)
				continue;

			cat->reduceHealth(_skillDamage,INJURY_EARTHQUAKE);
		}

		this->getParent()->runAction((CCShake::actionWithDuration(0.5f,10.0f)));
		_isSkillActivated = false;

		// Stop sound here
		SoundLoader::endSound(SKILL_EARTHQUAKE_SOUND);
    }
}
